package cn.com.generaldata.os.ws.impl.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.ioc.annotation.InjectName;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.pojo.BillboardIndustryReports;
import cn.com.generaldata.os.pojo.User;
import cn.com.generaldata.os.service.common.AutoPushingService;
import cn.com.generaldata.os.vo.BillboardIndustryVO;
import cn.com.generaldata.os.vo.BillboardRuleVO;
import cn.com.generaldata.os.vo.ReportVO;
import cn.com.generaldata.os.vo.RportOfIndustryVO;

@Component
@InjectName
public class AutoPushingAction {

	@Autowired
	private AutoPushingService autoPushingService;

	public List<BillboardRuleVO> queryAllBillboardRules(String title) {
		return toVoList(autoPushingService.queryAllBillboardRules(title));
	}

	public int queryBillboardCompletReposts(List<String> bIndustryReports, String users) {
		return autoPushingService.queryBillboardCompletReposts(bIndustryReports, users);

	}

	public List<RportOfIndustryVO> queryCompletReposts(List<String> bIndustryReports, String users) {
		return autoPushingService.queryCompletReposts(bIndustryReports, users);

	}

	/**
	 * 催报信息，待填报报表列表.
	 * @param bill
	 * @return
	 */
	@At("/company/billboard/toFillReportList")
	@Ok("jsp:/static/company/billboard/toFillReportList")
	public List<ReportVO> turnQueryReports(@Param("id") String bill, HttpServletRequest request, HttpSession session) {

		//当前用户ID
		cn.com.generaldata.os.vo.UserVO voUser = (cn.com.generaldata.os.vo.UserVO) session.getAttribute("user");
		request.setAttribute("id", bill);
		List<ReportVO> turnQueryReports = autoPushingService.turnQueryReports(voUser.getId(), bill);
		return turnQueryReports;
	}

	public List<BillboardIndustryVO> queryAllBillboardIndustrys(String title, String content, String unit,
			String operatorId) {
		return toVoIndustryList(autoPushingService.queryAllBillboardIndustrys(title, content, unit, operatorId));
	}

	private List<BillboardIndustryVO> toVoIndustryList(List<cn.com.generaldata.os.pojo.BillboardIndustry> pBRs) {
		List<BillboardIndustryVO> industrys = new ArrayList<BillboardIndustryVO>();
		for (cn.com.generaldata.os.pojo.BillboardIndustry pBR : pBRs) {
			industrys.add(toVoBillboardIndustry(pBR));
		}
		return industrys;
	}

	private BillboardIndustryVO toVoBillboardIndustry(cn.com.generaldata.os.pojo.BillboardIndustry pBR) {
		if (pBR != null) {
			List<String> bIndustryReports = new ArrayList<String>();
			if (pBR.getbIndustryReports() != null) {
				for (BillboardIndustryReports billboardIndustryReports : pBR.getbIndustryReports()) {
					bIndustryReports.add(billboardIndustryReports.getId());
				}
			}
			int queryBillboardCompletReposts = queryBillboardCompletReposts(bIndustryReports, pBR.getUsers());
			return new BillboardIndustryVO(pBR.getId(), pBR.getOperator().getId(), pBR.getBillboardTitle(),
					pBR.getBillboardContent(), pBR.getIndustryId(), new Byte(pBR.getDateType()).intValue(),
					pBR.getOffset(), pBR.getStatus(), pBR.getLastReadTime(), pBR.getReportSum(), pBR.getUsers(),
					pBR.getUserSum(), bIndustryReports, queryBillboardCompletReposts);
		}
		return null;
	}

	public String createBillboardRule(BillboardRuleVO rule) {
		return autoPushingService.createBillboardRule(toPojo(rule));
	}

	public void removeBillboardIndustry(List<String> ids) {
		for (String id : ids) {
			autoPushingService.removeBillboardRule(id);
		}
	}

	public String createBillboardIndustry(BillboardIndustryVO bIndustry,
			List<cn.com.generaldata.os.newpojo.ReportType> cbReportTypes, String users) {
		return autoPushingService.createBillboardIndustry(toPojoBIndustry(bIndustry), cbReportTypes, users,
				bIndustry.getReportEnd());
	}

	private cn.com.generaldata.os.pojo.BillboardIndustry toPojoBIndustry(BillboardIndustryVO bi) {
		User operator = new User();
		operator.setId(bi.getOperatorId());

		return new cn.com.generaldata.os.pojo.BillboardIndustry(bi.getId(), operator, bi.getBillboardTitle(),
				bi.getBillboardContent(), bi.getIndustryId(), new Integer(bi.getDateType()).byteValue(),
				bi.getOffset(), bi.getStatus(), bi.getLastReadTime(), bi.getReportSum(), bi.getUsers(), bi.getUserSum());
	}

	public void removeBillboardRule(String id) {
		autoPushingService.removeBillboardRule(id);
	}

	private BillboardRuleVO toVo(cn.com.generaldata.os.pojo.BillboardRule pBR) {
		return new BillboardRuleVO(pBR.getId(), pBR.getOperator().getId(), pBR.getBillboardTitle(),
				pBR.getBillboardContent(), Arrays.asList(pBR.getUserIds().split(",")),
				new Byte(pBR.getDateType()).intValue(), pBR.getOffset());
	}

	private List<BillboardRuleVO> toVoList(List<cn.com.generaldata.os.pojo.BillboardRule> pBRs) {
		List<BillboardRuleVO> rules = new ArrayList<BillboardRuleVO>();
		for (cn.com.generaldata.os.pojo.BillboardRule pBR : pBRs) {
			rules.add(toVo(pBR));
		}
		for (int i = 0; i < rules.size(); i++) {
			rules.get(i).setUserIds(null);
		}
		return rules;
	}

	private cn.com.generaldata.os.pojo.BillboardRule toPojo(BillboardRuleVO br) {
		User operator = new User();
		operator.setId(br.getOperatorId());

		return new cn.com.generaldata.os.pojo.BillboardRule(br.getId(), operator, br.getBillboardTitle(),
				br.getBillboardContent(), br.getUserIds().toString().replaceAll("\\[", "").replaceAll("\\]", ""),
				new Integer(br.getDateType()).byteValue(), br.getOffset());
	}

}
